<?php

namespace xing\yii2Linkage\models;

use Yii;

/**
 * This is the model class for table "region".
 *
 * @property int $regionId 主键
 * @property string $name 名称
 * @property int $parentId 父id
 * @property int $sorting 排序
 */
class Region extends \xing\helper\yii\MyActiveRecord
{
    /**
     * @var bool $cacheTree 缓存树数据
     */
    public static $cacheTree = true;

    /**
     * @var string $cacheTreeKey 缓存键名
     */
    public static $cacheTreeKey = 'Region:tree';

    // 区域最大数据
//    public static $maxNumber = 10000;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'region';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['parentId', 'sorting'], 'integer'],
            [['name'], 'string', 'max' => 30],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'regionId' => '主键',
            'name' => '名称',
            'parentId' => '上级区域',
            'sorting' => '排序',
        ];
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        // 删除树型缓存
        if (static::$cacheTree) Yii::$app->cache->delete(static::$cacheTreeKey);
    }

    /**
     * 读取两级城市
     * @param int $reginId 城市id
     * @return string 如广东深圳
     */
    public static function readTwoName($reginId)
    {
        $region = self::findOne($reginId);
        $parentRegion = isset($region->parentId) ? self::findOne($region->parentId) : null;
        return ($parentRegion ? $parentRegion->name : '') . ($region ? $region->name : '');
    }

    /**
     * 根据城市名读取城市信息
     * @param string $name 搜索关键字
     * @param bool $bool 是否左右自动加 % ，否则自己加
     * @return array|null|\yii\db\ActiveRecord|Region
     */
    public static function likeName($name, $bool = true)
    {
        !$bool && $name .= '%';
        return self::find()->where(['like', 'name', $name, $bool])->one();
    }



    /**
     * @param int $parentId
     * @param bool $showEmpty
     * @return array
     */
    public static function dropDownTrue($parentId = 0, $showEmpty = false)
    {
        $cacheKey = static::$cacheTreeKey . $parentId;
        if (static::$cacheTree) {
            $cache = Yii::$app->cache->get($cacheKey);
            if (!empty($cache)) {
                if ($showEmpty && is_string($showEmpty)) array_unshift($cache, $showEmpty);
                return $cache;
            }
        }

        $data = static::readCategoryTrue($parentId);
        $return = [];
        if ($showEmpty) $return[''] = is_string($showEmpty) ? $showEmpty : '一级行政区域';
        if (empty($data)) return $return;

        // 删除树型缓存
        $return = static::_dropDownTrue($data);
        if (static::$cacheTree) Yii::$app->cache->set($cacheKey, $return);

        return $return;
    }

    /**
     * @param $data
     * @param string $fh
     * @return array
     */
    public static function _dropDownTrue($data, $fh = '')
    {
        static $return, $nnn;
        $fh .= '| ';
        if (empty($data)) return [];
        foreach ($data as $v) {
            $return[$v['regionId']] =  $fh. $v['name'];
            if (isset($v['data']) && !empty($v['data'])) static::_dropDownTrue($v['data'], $fh);
            $nnn ++;
        }
        /*if ($nnn >= 10000) {
            exit('循环了10000次，应该没这么多数据吧？为避免造成无限循环，终止程序了');
        }*/
        return $return;
    }

    /**
     * 读取栏目树
     * @param int $parentId
     * @param bool $bindIndex
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function readCategoryTrue($parentId = 0, $bindIndex = false, $childrenKey = 'data')
    {
        $m = self::find()->where(['parentId' => $parentId])->asArray()->orderBy(['sorting' => SORT_DESC, 'regionId' => SORT_ASC]);
        if ($bindIndex) $m->indexBy('regionId');
        $data = $m->all();
        if (empty($data)) return $data;

//        static $nnn;
//        if (++$nnn > 10000) exit('无限循环？');

        # 获取子栏目：如果有下级结构，则读取下级结构，否则读取属性值
        foreach ($data as $k => $v) {
            $data[$k]['data'] = static::readCategoryTrue($v['regionId']);
        }
        return $data;
    }

    /**
     * 获取最上级的id
     * @param $regionId
     * @return int|null
     */
    public static function getTopId($regionId)
    {
        do {
            $region = static::findOne($regionId);
        } while ($region->parentId > 0);
        return $region ? $region->parentId : null;
    }

    /**
     * 获取所有子id
     * @param $regionId
     * @return array
     */
    public static function getChildIds($regionId)
    {
        $cacheKey = 'RCIds:' . $regionId;
        $ids = Yii::$app->cache->get($cacheKey) ?: [];
        if (empty($ids)) {
            $list = static::find()->where(['parentId' => $regionId])->all();
            $ids = array_merge($ids, array_column($list, 'regionId'));
            foreach ($list as $v) $ids = array_merge($ids, static::getChildIds($v->regionId));
            Yii::$app->cache->set($cacheKey, $ids);
        }
        return $ids;
    }
}
